//
//  HomeFlowStoreCatCell_style2.m
//  BaseProject
//
//  Created by 王洋 on 2020/11/30.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "EllipsePageControl.h"

#import "StoreCategoryCollectionCell.h"
#import "HomeFlowStoreCatCell_style2.h"

@interface HomeFlowStoreCatCell_style2 ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,EllipsePageControlDelegate>{
    
    CGSize _cellSize;
    UIEdgeInsets _collectionEdge;
}

@property (nonatomic, strong) UICollectionView *collectionview;

@property(nonatomic,strong) EllipsePageControl *myPageControl;


@property (nonatomic, strong) UIImageView *itemBGImageView;

@property (nonatomic, strong) UIView *bgView;

@end

@implementation HomeFlowStoreCatCell_style2

+ (CGFloat)calculateHeight {
    
    CGFloat collectionHeight =  ((TTScreenWidth - 20)/5)*1.1 + 20;
    return collectionHeight;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        [self layout];
    }
    return self;
}

- (void)layout {
    
    [self addSubview:self.itemBGImageView];
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.collectionview];
    [self addSubview:self.myPageControl];

    [self.itemBGImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.bottom.mas_equalTo(0);
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo(5);
    }];
    
    [self.collectionview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-5);
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    
    [self.myPageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.collectionview);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(TTScreenWidth - 30);
        make.height.mas_equalTo(20);
    }];
    [self layoutIfNeeded];

}

- (void)fillCellWithModel:(HomeADItemModel *)model {
    
    if (model.advBackImg.length > 0) {
        [self.itemBGImageView sd_setImageWithURL:[NSURL URLWithString:model.advBackImg] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    } else {
        [self.itemBGImageView setImage:[UIImage imageNamed:@"BAmarriage2"]];
    }
    
    self.dataArray = @[].mutableCopy;
    self.dataArray = model.sysAdvertisement.sysAdvEventList;
    
    if (self.dataArray.count > 5) {
        self.myPageControl.hidden = NO;
        self.myPageControl.numberOfPages = self.dataArray.count/5 + 1;
        
        if (self.dataArray.count > 0) {
            self.myPageControl.currentPage = 0;
        }
    } else {
        self.myPageControl.hidden = YES;
    }
    

    [self.collectionview reloadData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger currentPage = scrollView.contentOffset.x / (TTScreenWidth-20);
    double x = scrollView.contentOffset.x/(TTScreenWidth-20);
    
    if (x > currentPage) {
        currentPage ++;
    }
    
    self.myPageControl.currentPage = currentPage;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.bgView pp_clipCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:14];

    });
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    StoreCategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreCategoryCollectionCellID" forIndexPath:indexPath];
    cell.data = self.dataArray[indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [[StatisticManager shareManager]insertBrowseUserActiveQuantityWithType:@"3"];
    if (self.HomeFlowStoreCatCell_style2Block) {
        self.HomeFlowStoreCatCell_style2Block(indexPath.row);
    }
}

- (UIImageView *)itemBGImageView {
    if (!_itemBGImageView) {
        _itemBGImageView = [[UIImageView alloc] init];
        _itemBGImageView.contentMode = UIViewContentModeScaleToFill;
    }
    return _itemBGImageView;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.clipsToBounds = YES;
        _bgView.backgroundColor = UIColor.whiteColor;
    }
    return _bgView;
}

- (UICollectionView *)collectionview {
    
    if (!_collectionview) {
        CGFloat width = TTScreenWidth - 30;
        
        CGFloat cellWidth;
        cellWidth = width/5 - 4;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.minimumLineSpacing = 5;
        
        flowLayout.itemSize = CGSizeMake(cellWidth, cellWidth*1.1);

        _collectionview  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
        _collectionview.delegate = self;
        _collectionview.dataSource = self;
        _collectionview.pagingEnabled = YES;
        _collectionview.clipsToBounds = YES;
        _collectionview.showsHorizontalScrollIndicator = NO;
        _collectionview.showsVerticalScrollIndicator = NO;
        _collectionview.backgroundColor = [UIColor whiteColor];
        [_collectionview registerClass:[StoreCategoryCollectionCell class] forCellWithReuseIdentifier:@"StoreCategoryCollectionCellID"];
    }
    return _collectionview;
}

- (EllipsePageControl *)myPageControl {
    
    if (!_myPageControl) {
        _myPageControl = [[EllipsePageControl alloc] init];
        _myPageControl.numberOfPages = 6;
        _myPageControl.currentColor = UIColorHex(0xFF7200);
        _myPageControl.otherColor = UIColorHex(0xDDDDDD);

        _myPageControl.delegate=self;
        _myPageControl.tag=7777;

    }
    return _myPageControl;
}

@end
